package com.asiainfo.kbisheet.components.condition
{
	import com.asiainfo.kbisheet.control.IKbsControl;
	import com.asiainfo.kbisheet.control.MessageControl;
	import com.asiainfo.kbisheet.model.KbsModel;
	import com.asiainfo.kbisheet.vo.WhereVO;
	
	import mx.collections.ArrayCollection;
	import mx.core.FlexGlobals;
	
	import spark.components.HGroup;
	import spark.components.Label;
	import spark.components.VGroup;

	[Event(name="change")]
	[Event(name="updateConditionComplete")]
	public class ConditionItem extends HGroup
	{
		public var column:String;
		
		public var conditionId:String;
		
		/**预设值*/
		[Bindable]
		public var preset:String;
		
		[Bindable]
		protected var kbsModel:KbsModel = KbsModel.getInstance();
		
		protected var messageControl:MessageControl = new MessageControl();
		
		public var conditionXML:XML;
		
		/*需要在运行中根据配置文件实例化的控制器*/
		protected var kbsControl:IKbsControl;
		
		protected var conditionInputHandler:ArrayCollection = new ArrayCollection();
		
		/*条件类型*/
		protected var type:String;
		
		protected var _updateConditionCompleted:Boolean = false;
		
		/*新行*/
		public var newline:Boolean = false;
		
		public function ConditionItem(xml:XML)
		{
			super();
			/*把组件添加到列表中*/
			kbsModel.conditionItemHandler.addItem(this);
			
			conditionXML = xml;
			analyseConditionXML();
		}
		
		public function getAValue():String{
			for each(var conditionInput:ConditionInput in conditionInputHandler){
				var where:WhereVO = conditionInput.getWhereVO();
				if(where != null){					
					return where.value;
				}
			}
			return null;
		}
		
		public function getWheres():ArrayCollection{
			if(isShowCondition()){
				var wheres:ArrayCollection = new ArrayCollection();
				for each(var conditionInput:ConditionInput in conditionInputHandler){
					var where:WhereVO = conditionInput.getWhereVO();
					if(where != null){
						where.column = column;
						wheres.addItem(where);
					}
				}
				return wheres;
			}
			else{
				return null;
			}
		}
		
		public function getValue():String{
			for each(var conditionInput:ConditionInput in conditionInputHandler){
				var where:WhereVO = conditionInput.getWhereVO();
				if(where != null){
					return where.value;
				}
			}
			return null;
		}
		
		protected function isShowCondition():Boolean{
			return (column != null && column.length > 0);
		}
		
		/*包含排除值*/
		public function getExcludes():XMLList{
			var value:String = getAValue();
			if(conditionXML.excludes.(hasOwnProperty('@valid') && @valid == value).length()>0){
				return conditionXML.excludes.(hasOwnProperty('@valid') && @valid == value).exclude;
			}
			return null;
		}
		
		/*包含附加值*/
		public function getIncludes():XMLList{
			var value:String = getAValue();
			if(conditionXML.includes.(hasOwnProperty('@valid') && @valid == value).length()>0){
				return conditionXML.includes.(hasOwnProperty('@valid') && @valid == value).child("include");
			}
			return null;
		}
		
		
		
		/**
		 * 初始化数据
		 */
		public function initData():void{
			dispatchUpdateConditionComplete();
		}
		
		/**
		 * 当一个组件发生变更时，该组件是否需要变更。 
		 */
		public function needRefresh(conditionId:String):Boolean{
			var refreshXMLList:XMLList = conditionXML.refresh;
			for each(var refreshXML:XML in refreshXMLList){
				var change:String = refreshXML.@change;
				if(change == conditionId){
					return true;
				}
			}
			return false;
			
		}
		
		public function get updateConditionCompleted():Boolean{
			return _updateConditionCompleted;
		}
		
		override protected function createChildren():void {
			verticalAlign = "middle";
			
			var label1 : Label = new Label();
			var isFromTo:Boolean = (conditionXML.@equal=="from-to");
			
			label1.text = conditionXML.@title;
			addElement(label1);
			
			//绘制输入区
			var groupInput:VGroup = new VGroup();
	
			var groupFrom:HGroup = new HGroup();
			groupFrom.verticalAlign = "middle";
			
			if(isFromTo){
				var label_from : Label = new Label();
				label_from.text = "从";
				groupFrom.addElement(label_from);
			}
			var conditionInput :ConditionInput = createInputComponent();
			if(conditionInput!=null){
				conditionInput.id ="conditionInput1";
				
				if(isFromTo){
					conditionInput.equalSign = ">=";
				}
				else{
					conditionInput.equalSign = "=";
				}
				/*把组件添加到显示对象中*/
				groupFrom.addElement(conditionInput);
			}
			
			groupInput.addElement(groupFrom);
			
			if(isFromTo){
				var groupTo:HGroup = new HGroup();
				groupTo.verticalAlign = "middle";
				
				var label_to : Label = new Label();
				label_to.text = "到";
				groupTo.addElement(label_to);
				var conditionInput2 :ConditionInput = createInputComponent();
				if(conditionInput2!=null){
					conditionInput2.id ="conditionInput2";
					conditionInput2.equalSign = "<=";
					groupTo.addElement(conditionInput2);
				}
				groupInput.addElement(groupTo);
			}
			
			addElement(groupInput);
		}
		
		protected function updateMessage(message:String):void{
			messageControl.updateMessage(message);
		}
		
		/*分派更新条件完成事件*/
		protected function dispatchUpdateConditionComplete():void{
			if(!_updateConditionCompleted){
				_updateConditionCompleted = true;
				var newEvent:Event = new Event("updateConditionComplete");
				dispatchEvent(newEvent);
			}
		}
		
		private function createInputComponent():ConditionInput{
			var conditionInput:ConditionInput = ConditionInputFactory.create(this, type);			
			if(conditionInput){
				conditionInputHandler.addItem(conditionInput);
				conditionInput.addEventListener("change", onChange);
			}
			return conditionInput;
		}
		
		protected function onChange(event:Event):void{
			var newEvent:Event = new Event("change");
			dispatchEvent(newEvent);
		}
		
		/*分析XML*/
		private function analyseConditionXML():void{
			updateMessage("ConditionItem:analyseConditionXML:分析XML...");
			column = conditionXML.@column;
			conditionId = conditionXML.@id;
			type = conditionXML.@type;
			
			if(conditionXML.hasOwnProperty('@preset')){
				preset = conditionXML.@preset;
			}				
			
			if(FlexGlobals.topLevelApplication.parameters[column]){
				//trace(FlexGlobals.topLevelApplication.parameters[column]);
				preset = FlexGlobals.topLevelApplication.parameters[column];
			}
			
			if(FlexGlobals.topLevelApplication.parameters[conditionId]){
				//trace(FlexGlobals.topLevelApplication.parameters[conditionId]);
				preset = FlexGlobals.topLevelApplication.parameters[conditionId];
			}
			/*恒排除的值*/
			if(conditionXML.excludes.(!hasOwnProperty('@valid')).exclude.(hasOwnProperty('@value') && @value == preset).length() > 0){
				preset = null;
			}
			/*输入排除的值*/
			if(conditionXML.hasOwnProperty('input') && conditionXML.input.excludes.exclude.(hasOwnProperty('@value') && @value == preset).length() > 0){
				preset = null;
			}
			
			if(preset == ""){
				preset = null;
			}
			
			if(conditionXML.hasOwnProperty('@newline') && conditionXML.@newline == 'true'){
				newline = true;
			}
		}
	}
}